import java.io.*;
import java.util.Objects;

public class direCopy {
    public static void main(String[] args) throws IOException {
        copy("E:/360downloads");
    }
    public static void copy (String dir) throws IOException {
        File f1 = new File(dir);
        if (f1.isDirectory()){
            String todir = dir.substring(0,dir.lastIndexOf("/")+1)+"todir";
            copydir(dir,todir);
        }
        else{
            String todir = dir.substring(0,dir.lastIndexOf("."))+"copy"+dir.substring(dir.lastIndexOf("."));
            File f2 = new File(todir);
            f2.createNewFile();
            copyfile(f1,f2);
        }
    }
    public static void copydir(String dir,String todir) throws IOException{
        File f1 = new File(dir);
        File f2 = new File(todir);
        f2.mkdir();
        if (Objects.requireNonNull(f1.list()).length>0){
            for (String str : f1.list()){
                File f3 = new File(dir+"/"+str);
                if (f3.isDirectory()){
                    copydir(dir+"/"+str,todir+"/"+str);
                }
                else{
                    File f4 = new File(todir+"/"+str);
                    f4.createNewFile();
                    copyfile(f3,f4);
                }
            }
        }
    }
    public static void copyfile (File f1,File f2) throws IOException{
        InputStream inp = new FileInputStream(f1);
        byte[] bytes = new byte[inp.available()];
        inp.read(bytes);
        inp.close();
        OutputStream out = new FileOutputStream(f2);
        out.write(bytes);
        out.close();
    }
}
